package org.xshaokuang.loginandemail.email;

import cn.nukkit.item.Item;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class EmailData {
    private long timestamp;
    private int keepDays;
    private String text;
    private List<Item> items;
    private int flag;

    private String from;

    // 构造方法
    public EmailData(long timestamp, int keepDays, String text, List<Item> items, int flag, String from) {
        setTimestamp(timestamp);
        setKeepDays(keepDays);
        setText(text);
        setItems(items);
        setFlag(flag);
        setFrom(from);
    }

    // Getter 和 Setter 方法
    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public int getKeepDays() {
        return keepDays;
    }

    public void setKeepDays(int keepDays) {
        if (keepDays < 0) {
            throw new IllegalArgumentException("keepDays 不能为负数");
        }
        this.keepDays = keepDays;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text != null ? text : "";
    }

    public List<Item> getItems() {
        // 返回不可修改的列表保证封装性
        return Collections.unmodifiableList(items);
    }

    public void setItems(List<Item> items) {
        // 防御性拷贝防止外部修改
        this.items = items != null ? new ArrayList<>(items) : new ArrayList<>();
    }

    public int getFlag() {
        return flag;
    }

    public void setFlag(int flag) {
        if (flag < 0 || flag > 2) {
            throw new IllegalArgumentException("非法的标志位: " + flag);
        }
        this.flag = flag;
    }

    // 辅助方法
    public void addItem(Item item) {
        items.add(item);
    }

    public void removeItem(Item item) {
        items.remove(item);
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from != null ? from : "";
    }

    public void print() {
        System.out.println("EmailData{" +
                "timestamp=" + timestamp +
                ", keepDays=" + keepDays +
                ", text='" + text + '\'' +
                ", items=" + items +
                ", flag=" + flag +
                ", from='" + from + '\'' +
                '}');
    }
}

